#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

int main(){
    pid_t pid;
    int ret;

    pid = fork();
    if(pid > 0){
        exit(0);//父进程终止
    }

    pid = setsid();//创建新会话
    if(pid == -1){
        perror("setsid err");
        exit(1);
    }

    chdir("/home/lhy");

    umask(0022);//改变文件权限掩码

    close(STDIN_FILENO);//关闭文件描述符0
    int fd = open("/dev/null",O_RDWR);//此时fd=0
    if (fd == -1){
        perror("open error");
        exit(1);
    }

    dup2(fd,STDOUT_FILENO);//重定向stdout和stderr
    dup2(fd,STDERR_FILENO);

    while(1);//模拟守护进程业务

    return 0;
}